﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiYiTong.Rocket.Helper;
using YiYiTong.Rocket.Model;

namespace YiYiTong.Rocket.Business
{
    public class ExpressBiz
    {
        /// <summary>
        /// 快递实时查询
        /// </summary>
        /// <param name="com">查询的快递公司的编码</param>
        /// <param name="num">查询的快递单号</param>
        /// <param name="phone">寄件人或收件人手机号（顺丰单号必填）</param>
        /// <returns></returns>
        public static Tuple<bool, string, ExpressQueryResp> Query(string com, string num, string phone)
        {
            string customer = ConfigHelper.AppSetting<string>("express_customer");
            string key = ConfigHelper.AppSetting<string>("express_key");
            string url = ConfigHelper.AppSetting<string>("express_url");

            ExpressParam param = new ExpressParam()
            {
                com = com,
                num = num,
                phone = phone,
                resultv2 = "1"
            };

            string sign = Utils.Md5Encrypt(JsonHelper.Serialize(param) + key + customer, Encoding.UTF8).ToUpper();
            string data = string.Format("customer={0}&sign={1}&param={2}", customer, sign, JsonHelper.Serialize(param));
            LogHelper.Info("快递110实时查询,请求：" + data);
            string result = HttpHelper.HttpPost(url, data, "application/x-www-form-urlencoded");
            LogHelper.Info("快递110实时查询,响应：" + result);
            if (!string.IsNullOrEmpty(result))
            {
                var error = JsonHelper.DeserializeLower(result);
                if (error.ContainsKey("result") && error.ContainsKey("returncode"))
                    return new Tuple<bool, string, ExpressQueryResp>(false, error.GetValue<string>("message"), null);

                ExpressQueryResp respData = JsonHelper.Deserialize<ExpressQueryResp>(result);
                respData.stateMsg = EnumHelper.GetEnumItemDescription<ExpressState>(respData.state);
                return new Tuple<bool, string, ExpressQueryResp>(true, "", respData);
            }
            else
                return new Tuple<bool, string, ExpressQueryResp>(false, "快递查询数据为空", null);
        }
    }

    public class ExpressParam
    {
        public string com { get; set; }
        public string num { get; set; }
        public string phone { get; set; }
        public string resultv2 { get; set; }
    }

    public class ExpressQueryResp
    {
        /// <summary>
        /// 0在途中、1已揽收、2疑难、3已签收、4退签、5同城派送中、6退回、7转单
        /// </summary>
        public int state { get; set; }
        /// <summary>
        /// 状态描述
        /// </summary>
        public string stateMsg { get; set; }

        public string com { get; set; }

        public string express_name { get; set; }

        public string nu { get; set; }

        public List<ExpressData> data { get; set; }

    }

    public class ExpressData
    {
        public string context { get; set; }

        public string time { get; set; }

        public string ftime { get; set; }

        public string status { get; set; }
    }
}
